﻿using Hx.ADSyncPlatform.Infrastructure;
using SqlSugar;
using System;


namespace Hx.ADSyncPlatform.Db
{
    public class DbContext
    {
        public SqlSugarClient Db;   //用来处理事务多表查询和复杂的操作

        public DbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {

                ConnectionString = AppSettings.Configuration["DbConnection:ConnectionString"],
                DbType = Enum.Parse<DbType>(AppSettings.Configuration["DbConnection:DbType"]),
                IsAutoCloseConnection = true,
                IsShardSameThread = true,
                InitKeyType = InitKeyType.Attribute,
                //ConfigureExternalServices = new ConfigureExternalServices()
                //{
                //    DataInfoCacheService = new RedisCache()
                //},
                MoreSettings = new ConnMoreSettings()
                {
                    IsAutoRemoveDataCache = true
                }
            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql);
            };
        }


        public static DbContext Current
        {
            get
            {
                return new DbContext();
            }
        }
        public DbSet<T> DbSet<T>() where T : class, new()
        {
            return new DbSet<T>(Db);
        }





    }


}